<template>
  <div class="bigDiv">
    <el-upload
      class="filter-item"
      name="excelFile"
      :action="uploadUrl()"
      :on-error="uploadFalse"
      :on-success="uploadSuccess"
      :before-upload="beforeAvatarUpload"
      :limit="1"
      accept=".xlsx,.xls"
      :show-file-list="false"
      :file-list="fileList"
    >
      <el-button style="margin-left: 10px;" type="warning">导入</el-button>
    </el-upload>

  </div>
</template>

<script>
export default {
  name: 'Index',
  methods: {
    uploadSuccess(response, file, fileList) {
      console.log(response)
      if (response.code == 200) {
        this.$message({
          message: response.message,
          type: 'success'
        })
      } else {
        this.$message({
          message: response.message,
          type: 'error'
        })
      }
    },
    uploadFalse(response, file, fileList) {
      this.$message({
        message: '文件上传失败！',
        type: 'error'
      })
    },
    // 上传前对文件的大小的判断
    beforeAvatarUpload(file) {
      const extension = file.name.split('.')[1] === 'xls'
      const extension2 = file.name.split('.')[1] === 'xlsx'
      const isLt2M = file.size / 1024 / 1024 < 10
      if (!extension && !extension2) {
        this.$message({
          message: '上传模板只能是 xls、xlsx格式!',
          type: 'error'
        })
      }
      if (!isLt2M) {
        console.log('上传模板大小不能超过 10MB!')
        this.$message({
          message: '上传模板大小不能超过 10MB!',
          type: 'error'
        })
      }
      return extension || extension2 || extension3 || (extension4 && isLt2M)
    },
    uploadUrl: function() {
      return (
        '/upLoad'
      )
    }

  }
}
</script>

<style scoped lang="scss">
.filter-item{

}
.bigDiv{
  display: inline-block;
  margin-right: 16px;
  margin-left: 6px;
}
</style>
